package com.batch.quartz.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <h3>partitionjob</h3>
 * <p></p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2019-12-20 15:01
 **/
@Service
public class ImportUserServiceImpl {
    private static final Logger LOGGER = LoggerFactory.getLogger(ImportUserServiceImpl.class);

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    @Qualifier("importUserJob")
    private Job importUserJob;

//
//    public ImportUserServiceImpl(JobLauncher jobLauncher, Job importUserJob) {
//        this.jobLauncher = jobLauncher;
//        this.importUserJob = importUserJob;
//    }

    public void executeBatchJob(String taskId) {
//        String fileName = "person.txt";
        JobParameters jobParameters = new JobParametersBuilder()
                .addString("taskId", taskId)
//                .addString("fileName", fileName)
                .addString("uuid", UUID.randomUUID().toString().replace("-",""))
                .toJobParameters();
        JobExecution jobExecution = null;
        try {
            jobExecution = jobLauncher.run(importUserJob, jobParameters);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("执行任务失败: {}", e);
        }
        LOGGER.info("导入用户任务 : 执行结果: {}", jobExecution.getExitStatus().toString());
    }
}
